package com.hmall.item.listener;

import com.hmall.item.constants.MqConstant;
import com.hmall.item.domain.dto.ItemMQDto;
import com.hmall.item.domain.dto.ItemDTO;
import com.hmall.item.es.EsOperating;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class ItemMqListener {

    private final EsOperating esOperating;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstant.ITEM_QUEUE),
            exchange = @Exchange(name = MqConstant.ITEM_EXCHANGE, delayed = "false"),
            key = MqConstant.ITEM_ROUTING_KEY
    ))
    public void listenerItemMessage(ItemMQDto itemMQDto) {
        if (itemMQDto.getItemDTO().getId() == null) return;
        switch (itemMQDto.getOperate()) {
            case ADD: //添加
                esOperating.addItemToIndex(itemMQDto.getItemDTO());
                break;
            case DELETE://删除
                esOperating.removeItemByIndex(itemMQDto.getItemDTO());
                break;
            case UPDATE://更新
                esOperating.updateItemByIndex(itemMQDto.getItemDTO());
                break;
            default:
                log.error("未知的操作类型！");
        }
    }

}
